package org.holynite.fieldmanage.repositories.impl

import org.holynite.fieldmanage.entities.CustomerType
import org.holynite.fieldmanage.persistence.CustomerTypeEAO
import org.holynite.fieldmanage.repositories.BaseEAO

import javax.ejb.Stateless

/**
 * Created by midnite on 15/1/20.
 */
@Stateless(name = "customerTypeEao")
class CustomerTypeEAOImpl extends BaseEAO implements CustomerTypeEAO {
	@Override
	CustomerType findTypeByName(String name) {
		def result
		try {
			def query = em.createQuery("select ct from CustomerType as ct where ct.customerTypeName=:name")
			query.setParameter("name", name)
			result = query.singleResult as CustomerType
		} catch (Exception e) {
			result = null
		}
		result
	}

	@Override
	List<CustomerType> searchCustomerType(String keyword) {
		def result
		try {
			if (keyword.length() > 0) {
				def query = em.createQuery("select ct from CustomerType as ct where ct.customerTypeName like :keyword")
				query.setParameter("keyword", "%${keyword}%".toString())
				result = query.resultList as List<CustomerType>
			} else {
				def query = em.createQuery("select ct from CustomerType as ct")
				result = query.resultList as List<CustomerType>
			}
		} catch (Exception e) {
			result = []
		}
		result
	}
}
